package tinglema.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import tinglema.server.common.R;
import tinglema.server.dao.RankListDao;
import tinglema.server.entity.domain.RankList;
import tinglema.server.entity.domain.SongList;
import tinglema.server.service.RankListService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 歌单评分(RankList)表服务实现类
 *
 * @author delucia
 */
@Service("rankListService")
public class RankListServiceImpl extends ServiceImpl<RankListDao, RankList> implements RankListService {
    @Resource
    private RankListDao rankMapper;
    @Override
    public Long selectScoreSum(Long songListId) {
        // 评分总人数如果为 0，则返回0；否则返回计算出的结果
        LambdaQueryWrapper<RankList> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RankList::getSongListId,songListId);
        Long rankNum = baseMapper.selectCount(queryWrapper);
        return  (rankNum == 0) ? 0 : rankMapper.selectScoreSum(songListId) / rankNum;
    }
}

